<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>js测试</title>

  <style>
    table {
      border-collapse: collapse;
      flex: 1px;
      border-radius: 5px;
      width: 100%;
      margin: 10px auto;
      border: 1px solid #000000;
      /* background-color: #328ef4; */
      /* color: #c7dafb; */
    }

    table td,
    th {
      /* padding: 5px;
    	width: 33%; */
      text-align: center;
      border: 1px solid #000000;
      vertical-align: middle;
      height: 41px;
      /* font-size: 15px; */
      /* width: 80%; */
    }

    #operation {
      height: 200px;

      border: 1px solid black;
    }

    .btn {
      margin-bottom: 10px;
      height: 80px;
      border: 1px solid blue;
      position: relative;
      top: 95px;
    }

    .butt {
      border: 1px solid;
      border-radius: 9px;
      width: 100px;
      height: 30px;
      background-color: darkorange;
      position: relative;
      top: 44px;
      margin-left: 25px;
    }

    .add_student {
      display: none;
      position: absolute;
      top: 25%;
      left: 25%;
      width: 55%;
      height: 55%;

      border: 1px solid orange;
      background-color: white;
      z-index: 1002;
      overflow: auto;
    }

    .container {
      position: absolute;
      width: 100%;
      height: 100%;

    }


    .black_overlay {
      display: none;
      position: absolute;
      top: 0%;
      left: 0%;
      width: 100%;
      height: 100%;
      background-color: black;
      z-index: 1001;
      -moz-opacity: 0.8;
      opacity: .80;
      filter:
        alpha(opacity=88);
    }

    .close {
      position: relative;
      right: 1px;
      background-color: deepskyblue;
      height: 30px;
      width: 90px;
      z-index: 9999;


    }

    .student {
      position: relative;
      height: 100%;
    }

    #form {
      position: relative;
      height: 60%;
    }

    .student1 {
      position: relative;
      top: 90px;
    }
  </style>
</head>

<body>

  <div class='container'>

    <div id='operation'>
      <div class="student1">
        <form action="#" id='form1'>
          姓名：<label for="name"></label><input type="text" name="name" id="name1">
          年龄：<label for="age"></label><input type="number" name="age" id="age1">
        </form>
      </div>
      <div class="btn">
        <button class="butt" id='add_student' onclick="add_student()">+新增学生</button>
        <button class="butt" id='select_student' onclick="select_student()">查询</button>
      </div>
    </div>

    <div id='shiguiwu'>
      <table>
        <thead>
          <tr>
            <th>编号</th>
            <th>姓名</th>
            <th>性别</th>
            <th>年龄</th>
            <th>操作</th>
          </tr>
        </thead>
        <tbody>
          <!-- <tr>
            <td>1</td>
            <td>小明</td>
            <td>男</td>
            <td>18</td>
            <td>删除</td>
          </tr> -->
        </tbody>

      </table>


    </div>

  </div>
  <div class="add_student">

    <div
      style="position: relative;width: 89%; float: left;text-align: center;background-color: greenyellow;height: 30px;font-size: 22px;">
      新增学生</div>
    <div style="position: relative;width: 11%; float: right;"><button class="close" onclick="close1()">关闭</button>
    </div>

    <div class="student">
      <form action="#" id='form'>
        姓名：<label for="name"></label><input type="text" name="name" id="name"><br>
        性别：<label for="male"></label>男<input type="radio" name="sex" id="male" value="男" />
        <label for="fale"></label>女<input type="radio" name="sex" id="fale" value="女" /><br>
        年龄：<label for="age"></label><input type="number" name="age" id="age"><br>

        <button type="button" onclick="submit1()"
          style="width: 100px;background-color: indianred;margin-top: 10px;border: 1px solid; border-radius:6px;margin-right: 3px;">保存</button>
      </form>

    </div>



  </div>

  <div class="black_overlay">

  </div>


  <script>
    console.log('table -- test');

    const table_list = []

    function add_student() {
      const add_model = document.getElementsByClassName('add_student')
      add_model[0].style.display = 'block'
      const block = document.getElementsByClassName('black_overlay')
      block[0].style.display = 'block'
    }

    function close1() {

      console.log('关闭·····················');

      const add_model = document.getElementsByClassName('add_student')
      add_model[0].style.display = 'none'
      const block = document.getElementsByClassName('black_overlay')
      block[0].style.display = 'none'
    }



    function submit1() {

      const form = document.getElementById('form')
      const name = document.getElementById('name').value
      const age = document.getElementById('age').value
      const male = document.getElementById('male')
      const fale = document.getElementById('fale')
      let sex = ''
      if (male.checked) {
        sex = '男'
      } else if (fale.checked) {
        sex = '女'
      }

      strToHtml({
        name,
        age,
        sex
      })




      close1()
      form.reset()
      return false
    }

    function strToHtml(props) {
      let id = ''
      if (table_list.length === 0) {
        id = 1
        props.id = 1
        table_list.push(props)
      } else {

        id = table_list[0].id + 1
        props.id = id
        table_list.unshift(props)
      }
      arrTohtml(table_list)

    }

    function arrTohtml(arr) {
      const html = table_list.map((e, index) => `
       <tr>
         <td>${e.id}</td>
         <td>${e.name}</td>
         <td>${e.sex}</td>
         <td>${e.age}</td>
         <td><button onclick='delete1(event,${index})'>删除</button></td>
       </tr>


       `).join('')
      const parent = document.querySelectorAll('#shiguiwu > table > tbody')
      parent[0].innerHTML = html


    }

    delete1 = (event, index) => {
      console.log('delete', index)
      const td = event.target.parentElement
      const tr = td.parentElement
      const tbody = tr.parentElement
      tbody.removeChild(tr)

      table_list.splice(index, 1)
    }

    function select_student() {
      const name = document.getElementById('name1').value
      const age = document.getElementById('age1').value
      if (name != '' && name != undefined) {
        table_list.filter(e1 => e1.name.length > 4)
      } else if (age != '' && age != undefined) {
        table_list.filter(e => e.age === age)
      }
      arrTohtml(table_list)


    }

    let zzz = setInterval(() => {
      console.log(1);
      setTimeout(() => {
        console.log(2);


        if (zzz) {
          console.log(zzz);
          clearInterval(zzz)
          console.log("删除后-----------------------------");
        }

      }, 2005)

    }, 1000);
    

    function request(url, data = {}, method = 'GET', contentType) {
      const promise = new Promise((resolve, reject) => {
        const handler = () => {
          if (this.readyState !== 4) {
            return;
          }
          if (this.status === 200) {
            resolve(JSON.parse(this.responseText));
          } else {
            reject(new Error(this.statusText));
          }
        };
        const client = new XMLHttpRequest();
        client.open(method, url);
        client.onreadystatechange = handler;
        client.responseType = "json";
        if (contentType) {//application/json
          data = JSON.stringify(data);
          client.setRequestHeader("Content-type", contentType);
        }
        // client.setRequestHeader("Accept", "application/json");
        client.send(data);

      });

      return promise;
    }


    request('http://127.0.0.1:8096/sign/getUserInfo',{id:'1212'})
    .then(res =>console.log(res)
    )

  </script>
</body>

</html>